package com.cookie.framework.adapter;

import android.view.ViewGroup;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.framework.adapter
 * 文件名:    CommonAdapter
 * 创建者:    cookie
 * 创建时间:   2020/1/11 23:26
 * 描述:      万能的适配器
 */

public class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {

    private List<T> list;
    private OnBindDataListener<T> onBindDataListener;
    private OnMoreBindDataListener<T> onMoreBindDataListener;

    public CommonAdapter(List<T> list, OnBindDataListener<T> onBindDataListener) {
        this.list = list;
        this.onBindDataListener = onBindDataListener;
    }

    public CommonAdapter(List<T> list, OnMoreBindDataListener<T> onMoreBindDataListener) {
        this.list = list;
        this.onBindDataListener = onMoreBindDataListener;
        this.onMoreBindDataListener = onMoreBindDataListener;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int layoutId = onBindDataListener.getLayoutId(viewType);
        CommonViewHolder viewHolder = CommonViewHolder.getViewHolder(parent, layoutId);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, int position) {
        onBindDataListener.onBindViewHolder(list.get(position), holder
                , getItemViewType(position), position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (onMoreBindDataListener != null) {
            return onMoreBindDataListener.getItemType(position);
        }
        return 0;
    }

    //绑定数据
    public interface OnBindDataListener<T> {
        void onBindViewHolder(T model, CommonViewHolder viewHolder, int type, int position);

        int getLayoutId(int type);
    }

    //绑定多类型的数据
    public interface OnMoreBindDataListener<T> extends OnBindDataListener<T> {
        int getItemType(int position);
    }
}
